#!/bin/bash

echo "=== Grafana 测试开始 ==="

# 启动 Grafana 服务
echo "启动 Grafana 服务..."
docker-compose -f grafana-compose.yml up -d

# 等待 Grafana 启动
echo "等待 Grafana 启动..."
sleep 20

# 检查容器状态
if ! docker ps | grep -q "test-grafana"; then
    echo "❌ Grafana 容器启动失败"
    exit 1
fi

echo "✅ Grafana 容器启动成功"

# 测试健康检查
echo "测试健康检查..."
HEALTH_RESPONSE=$(curl -s http://localhost:3000/api/health)
if echo "$HEALTH_RESPONSE" | grep -q '"database":"ok"'; then
    echo "✅ 健康检查通过"
else
    echo "❌ 健康检查失败"
    echo "健康检查响应: $HEALTH_RESPONSE"
    exit 1
fi

# 测试登录接口
echo "测试登录接口..."
LOGIN_RESPONSE=$(curl -s -X POST \
  -H "Content-Type: application/json" \
  -d '{"user":"admin","password":"admin123"}' \
  http://localhost:3000/login)

if echo "$LOGIN_RESPONSE" | grep -q '"message":"Logged in"'; then
    echo "✅ 登录接口正常"
else
    echo "❌ 登录接口异常"
    echo "登录响应: $LOGIN_RESPONSE"
fi

# 获取认证 Token
echo "获取认证 Token..."
TOKEN_RESPONSE=$(curl -s -X POST \
  -H "Content-Type: application/json" \
  -d '{"name":"test-token","role":"Admin"}' \
  -u admin:admin123 \
  http://localhost:3000/api/auth/keys)

if echo "$TOKEN_RESPONSE" | grep -q '"key"'; then
    TOKEN=$(echo "$TOKEN_RESPONSE" | grep -o '"key":"[^"]*"' | cut -d'"' -f4)
    echo "✅ Token 获取成功"
else
    echo "⚠️  Token 获取失败，使用基础认证"
    TOKEN=""
fi

# 测试数据源 API
echo "测试数据源 API..."
if [ -n "$TOKEN" ]; then
    DS_RESPONSE=$(curl -s -H "Authorization: Bearer $TOKEN" http://localhost:3000/api/datasources)
else
    DS_RESPONSE=$(curl -s -u admin:admin123 http://localhost:3000/api/datasources)
fi

if echo "$DS_RESPONSE" | grep -q '\[\]' || echo "$DS_RESPONSE" | grep -q '"name"'; then
    echo "✅ 数据源 API 正常"
    DS_COUNT=$(echo "$DS_RESPONSE" | grep -o '"name"' | wc -l)
    echo "数据源数量: $DS_COUNT"
else
    echo "❌ 数据源 API 异常"
    echo "数据源响应: $DS_RESPONSE"
fi

# 测试仪表板 API
echo "测试仪表板 API..."
if [ -n "$TOKEN" ]; then
    DASH_RESPONSE=$(curl -s -H "Authorization: Bearer $TOKEN" http://localhost:3000/api/search)
else
    DASH_RESPONSE=$(curl -s -u admin:admin123 http://localhost:3000/api/search)
fi

if echo "$DASH_RESPONSE" | grep -q '\[\]' || echo "$DASH_RESPONSE" | grep -q '"title"'; then
    echo "✅ 仪表板 API 正常"
    DASH_COUNT=$(echo "$DASH_RESPONSE" | grep -o '"title"' | wc -l)
    echo "仪表板数量: $DASH_COUNT"
else
    echo "❌ 仪表板 API 异常"
    echo "仪表板响应: $DASH_RESPONSE"
fi

# 测试用户 API
echo "测试用户 API..."
if [ -n "$TOKEN" ]; then
    USER_RESPONSE=$(curl -s -H "Authorization: Bearer $TOKEN" http://localhost:3000/api/users)
else
    USER_RESPONSE=$(curl -s -u admin:admin123 http://localhost:3000/api/users)
fi

if echo "$USER_RESPONSE" | grep -q '"login"'; then
    echo "✅ 用户 API 正常"
    USER_COUNT=$(echo "$USER_RESPONSE" | grep -o '"login"' | wc -l)
    echo "用户数量: $USER_COUNT"
else
    echo "❌ 用户 API 异常"
fi

# 测试组织 API
echo "测试组织 API..."
if [ -n "$TOKEN" ]; then
    ORG_RESPONSE=$(curl -s -H "Authorization: Bearer $TOKEN" http://localhost:3000/api/orgs)
else
    ORG_RESPONSE=$(curl -s -u admin:admin123 http://localhost:3000/api/orgs)
fi

if echo "$ORG_RESPONSE" | grep -q '"name"'; then
    echo "✅ 组织 API 正常"
else
    echo "❌ 组织 API 异常"
fi

# 测试插件 API
echo "测试插件 API..."
if [ -n "$TOKEN" ]; then
    PLUGIN_RESPONSE=$(curl -s -H "Authorization: Bearer $TOKEN" http://localhost:3000/api/plugins)
else
    PLUGIN_RESPONSE=$(curl -s -u admin:admin123 http://localhost:3000/api/plugins)
fi

if echo "$PLUGIN_RESPONSE" | grep -q '\[\]' || echo "$PLUGIN_RESPONSE" | grep -q '"id"'; then
    echo "✅ 插件 API 正常"
    PLUGIN_COUNT=$(echo "$PLUGIN_RESPONSE" | grep -o '"id"' | wc -l)
    echo "插件数量: $PLUGIN_COUNT"
else
    echo "❌ 插件 API 异常"
fi

# 测试设置 API
echo "测试设置 API..."
if [ -n "$TOKEN" ]; then
    SETTINGS_RESPONSE=$(curl -s -H "Authorization: Bearer $TOKEN" http://localhost:3000/api/admin/settings)
else
    SETTINGS_RESPONSE=$(curl -s -u admin:admin123 http://localhost:3000/api/admin/settings)
fi

if echo "$SETTINGS_RESPONSE" | grep -q '"default_theme"'; then
    echo "✅ 设置 API 正常"
else
    echo "❌ 设置 API 异常"
fi

# 性能测试 - API 响应时间
echo "执行 API 性能测试..."
START_TIME=$(date +%s%N)
if [ -n "$TOKEN" ]; then
    curl -s -H "Authorization: Bearer $TOKEN" http://localhost:3000/api/health > /dev/null
else
    curl -s -u admin:admin123 http://localhost:3000/api/health > /dev/null
fi
END_TIME=$(date +%s%N)
LATENCY=$(( (END_TIME - START_TIME) / 1000000 ))  # 转换为毫秒
echo "API 响应延迟: ${LATENCY}ms"

# 检查内存使用
echo "检查内存使用情况..."
MEMORY_USAGE=$(docker stats test-grafana --no-stream --format "table {{.MemUsage}}" | tail -n 1)
echo "内存使用: $MEMORY_USAGE"

echo "=== Grafana 测试完成 ==="

# 清理 Token
if [ -n "$TOKEN" ]; then
    echo "清理测试 Token..."
    curl -s -X DELETE -u admin:admin123 "http://localhost:3000/api/auth/keys/$TOKEN" > /dev/null
fi

# 选择是否停止服务
read -p "是否停止 Grafana 服务? (y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    docker-compose -f grafana-compose.yml down
    echo "Grafana 服务已停止"
fi